package czk.concrent.juc.countDOwnLatch;

import java.util.concurrent.CountDownLatch;

/**
 * CountDownLatch计数器代码演示
 * @Author:ChenZhangKun
 * @Date: 2020/7/25 14:23
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        // 创建计数器
        CountDownLatch countDownLatch=new CountDownLatch(3);

        // 三个线程
        new Thread(()->{

            try {
                Thread.sleep(1000);
                System.out.println("计数器减1");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
        }).start();
        new Thread(()->{

            try {
                Thread.sleep(2000);
                System.out.println("计数器减1");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
        }).start();
        new Thread(()->{

            try {
                Thread.sleep(3000);
                System.out.println("计数器减1");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
        }).start();
        // 此时主线程会一直阻塞,直到计数器减为0
        countDownLatch.await();
    }
}
